
import http from "@/utils/http"
/**
 * 佳博打印机打印方法
 *
 * @param {string} mrlCode 物料编号
 * @param {string} mrlName 物料名称
 * @param {string} lotCode 批次号
 * @param {int} qty 数量
 * @param {string} barCode 容器编号
 */
function printToJiaBo(mrlCode, mrlName, lotCode, qty, barCode, callback) {


    if (mrlCode != '测试打印连接') {
        //保存到localStorage
        let backUpPrintParams = {
            code: mrlCode,
            name: mrlName,
            lot: lotCode,
            num: qty,
            con: barCode
        }
        localStorage.setItem("backUpPrintParams", JSON.stringify(backUpPrintParams));
    }

    // window.api.showProgress({
    //     style: 'default',
    //     animationType: 'fade',
    //     title: '努力加载中...',
    //     text: '请稍后...',
    //     modal: true
    // });
    // http.post({
    //     url: "bogePadWmsController!getExpireDate.m",
    //     data: {
    //         mrlCode: mrlCode,
    //         lotCode: lotCode
    //     },
    //     config: {
    //         dataType: "json",
    //     }
    // }).then((ret2) => {
    //     if (ret2.errCode == 0) {
            let privacy = window.api.require("ble");
            let printModule = window.api.require('moduleDemo');
            let uuid = localStorage.getItem('uuid');
            if (!uuid) {
                window.api.alert({
                    title: '打印失败',
                    msg: '在个人中心-选择打印机IP中设置打印机后可恢复打印条码功能！'
                });
            } else {
                privacy.initManager(function (ret) {
                    if (ret.state == "poweredOn") {
                        let params = {
                            mac: uuid,
                            code: mrlCode,
                            name: mrlName,
                            lot: lotCode,
                            num: qty,
                            con: barCode
                        }
                        // params.mac = uuid;
                        console.log("打印的参数======" + JSON.stringify(params));
                        printModule.printData2(params, function (ret) {
                            console.log("打印的结果======" + JSON.stringify(ret));
                            window.api.toast({ msg: ret.result, location: 'middle' });
                        });
                        if (callback) {
                            callback();
                        }
                    } else {
                        window.api.alert({
                            title: '打印失败',
                            msg: '请开启蓝牙！'
                        });
                    }
                })
            }
            // window.api.hideProgress();
        // } else {
        //     let privacy = window.api.require("ble");
        //     let printModule = window.api.require('moduleDemo');
        //     let uuid = localStorage.getItem('hgzyUuid');
        //     if (!uuid) {
        //         window.api.alert({
        //             title: '打印失败',
        //             msg: '在个人中心-选择打印机IP中设置打印机后可恢复打印条码功能！'
        //         });
        //     } else {
        //         privacy.initManager(function (ret) {
        //             if (ret.state == "poweredOn") {
        //                 let params = {
        //                     mac: uuid,
        //                     code: mrlCode,
        //                     name: newMrlName,
        //                     lot: lotCode,
        //                     num: qty,
        //                     con: barCode
        //                 }
        //                 // params.mac = uuid;
        //                 console.log("打印的参数======" + JSON.stringify(params));
        //                 printModule.printData2(params, function (ret) {
        //                     console.log("打印的结果======" + JSON.stringify(ret));
        //                     window.api.toast({ msg: ret.result, location: 'middle' });
        //                 });
        //                 if (callback) {
        //                     callback();
        //                 }
        //             } else {
        //                 window.api.alert({
        //                     title: '打印失败',
        //                     msg: '请开启蓝牙！'
        //                 });
        //             }
        //         })
        //     }
        //     window.api.hideProgress();
        // }
    // }).catch(() => {

    //     window.api.hideProgress();
    // }
    // )


};

function printToJiaBoForProd(mrlCode, mrlName, lotCode, qty, barCode, prdDate, callback) {
    let newMrlName = '';
    if (mrlName) {
        if (mrlName.length > 35) {
            newMrlName = mrlName.substring(0, 35);
        } else {
            newMrlName = mrlName;
        }
    }

    //保存到localStorage
    let backUpPrintParams = {
        code: mrlCode,
        name: newMrlName,
        lot: lotCode,
        num: qty,
        con: barCode
    }
    localStorage.setItem("backUpPrintParams", JSON.stringify(backUpPrintParams));


    window.api.showProgress({
        style: 'default',
        animationType: 'fade',
        title: '努力加载中...',
        text: '请稍后...',
        modal: true
    });
    http.post({
        url: "bogePadWmsController!getExpireDate.m",

        data: {
            mrlCode: mrlCode,
            lotCode: lotCode,
            prdDate: prdDate
        },
        config: {
            dataType: "json",
        }
    }).then((ret2) => {
        if (ret2.errCode == 0) {
            let privacy = window.api.require("ble");
            let printModule = window.api.require('moduleDemo');
            let uuid = localStorage.getItem('hgzyUuid');
            if (!uuid) {
                window.api.alert({
                    title: '打印失败',
                    msg: '在个人中心-选择打印机IP中设置打印机后可恢复打印条码功能！'
                });
            } else {
                privacy.initManager(function (ret) {
                    if (ret.state == "poweredOn") {
                        let params = {
                            mac: uuid,
                            code: mrlCode,
                            name: newMrlName,
                            lot: lotCode + "   " + ret2.data,
                            num: qty,
                            con: barCode
                        }
                        // params.mac = uuid;
                        console.log("打印的参数======" + JSON.stringify(params));
                        printModule.printData2(params, function (ret) {
                            console.log("打印的结果======" + JSON.stringify(ret));
                            window.api.toast({ msg: ret.result, location: 'middle' });
                        });
                        if (callback) {
                            callback();
                        }
                    } else {
                        window.api.alert({
                            title: '打印失败',
                            msg: '请开启蓝牙！'
                        });
                    }
                })
            }
            window.api.hideProgress();
        } else {
            let privacy = window.api.require("ble");
            let printModule = window.api.require('moduleDemo');
            let uuid = localStorage.getItem('hgzyUuid');
            if (!uuid) {
                window.api.alert({
                    title: '打印失败',
                    msg: '在个人中心-选择打印机IP中设置打印机后可恢复打印条码功能！'
                });
            } else {
                privacy.initManager(function (ret) {
                    if (ret.state == "poweredOn") {
                        let params = {
                            mac: uuid,
                            code: mrlCode,
                            name: newMrlName,
                            lot: lotCode,
                            num: qty,
                            con: barCode
                        }
                        // params.mac = uuid;
                        console.log("打印的参数======" + JSON.stringify(params));
                        printModule.printData2(params, function (ret) {
                            console.log("打印的结果======" + JSON.stringify(ret));
                            window.api.toast({ msg: ret.result, location: 'middle' });
                        });
                        if (callback) {
                            callback();
                        }
                    } else {
                        window.api.alert({
                            title: '打印失败',
                            msg: '请开启蓝牙！'
                        });
                    }
                })
            }
            window.api.hideProgress();
        }
    }).catch(() => {
        window.api.hideProgress();

    })

}


function printToJiaBoAndRefresh(mrlCode, mrlName, lotCode, qty, barCode, isRefresh) {
    if (mrlCode != '测试打印连接') {
        let backUpPrintParams = {
            code: mrlCode,
            name: mrlName,
            lot: lotCode,
            num: qty,
            con: barCode
        }
        localStorage.setItem("backUpPrintParams", JSON.stringify(backUpPrintParams));
    }
    window.api.showProgress({
        style: 'default',
        animationType: 'fade',
        title: '努力加载中...',
        text: '请稍后...',
        modal: true
    });
    let privacy = window.api.require("ble");
    let printModule = window.api.require('moduleDemo');
    let uuid = localStorage.getItem('hgzyUuid');
    if (!uuid) {
        window.api.alert({
            title: '打印失败',
            msg: '在个人中心-选择打印机IP中设置打印机后可恢复打印条码功能！'
        });
    } else {
        privacy.initManager(function (ret) {
            if (ret.state == "poweredOn") {
                let params = {
                    mac: uuid,
                    code: mrlCode,
                    name: mrlName,
                    lot: lotCode,
                    num: qty,
                    con: barCode
                }
                // params.mac = uuid;
                // console.log("打印的参数======" + JSON.stringify(params));
                printModule.printData2(params, function (ret) {
                    // console.log("打印的结果======" + JSON.stringify(ret));
                    window.api.toast({ msg: ret.result, location: 'middle' });
                    if (isRefresh) {
                        window.location.reload();
                    }
                });
            } else {
                window.api.alert({
                    title: '打印失败',
                    msg: '请开启蓝牙！'
                });
            }
        })
    }
    window.api.hideProgress();
}

export {
    printToJiaBo
}
